from selenium import webdriver
from selenium.webdriver.common.by import By

wd = webdriver.Edge()
wd.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined
    })
  """
})
wd.implicitly_wait(10)
url = "https://www.12306.cn/index/"
wd.get(url)


ele_from_where = wd.find_element(By.CSS_SELECTOR,'#fromStationText')
ele_to_where = wd.find_element(By.CSS_SELECTOR, '#toStationText')

ele_from_where.clear()
ele_from_where.send_keys("北京")
wd.find_element(By.CSS_SELECTOR,'#citem_2').click()

ele_to_where.click()
ele_to_where.send_keys("天津")
wd.find_element(By.CSS_SELECTOR,'#citem_1').click()

searchBtn = wd.find_element(By.CSS_SELECTOR,'#search_one')
searchBtn.click()
#
train_items = wd.find_elements(By.CSS_SELECTOR,"#queryLeftTable > tr")
print(len(train_items))
print(train_items)
for num in range(len(train_items)):
    if num % 2 ==0:
        continue
    train_name = train_items[num].find_element(By.XPATH,'/tr/td[1]/ div / div[1] / div / a').text
    start_time = train_items[num].find_element(By.XPATH,'/tr/td[1]/div/div[3]/strong[1]').text
    print(train_name,'---',start_time)
